<?php

namespace App\Admin\Controllers;

use App\Accounts;
use App\Models\MetaSwitch;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SwitchController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'MetaSwitch';

    /**
     * Make a table builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $table = new Grid(new MetaSwitch());

        $table->column('id', __('Id'));
        $table->column('appid', __('Appid'));
        $table->column('location', __('排除地区'));
        $table->column('allow_location', __('允许地区'));
        $table->column('总数')->display(function () {
            return Accounts::where('appId',$this->appid)->count();
        });

        $table->column('switch', __('配置开关'))->switch();
        $table->column('notice_switch', __('通知开关'))->switch();
        $table->column('use_cloak', __('使用cloak'))->switch();
        $table->column('notice', __('备注'));

        return $table;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(MetaSwitch::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('appid', __('Appid'));
        $show->field('switch', __('配置开关'));
        $show->field('notice_switch', __('通知开关'));
        $show->field('use_cloak', __('使用cloak'));
        $show->field('location', __('排除地区'));
        $show->field('allow_location', __('允许地区'));
        $show->field('notice', __('备注'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new MetaSwitch());

        $form->text('appid', __('Appid'));
        $form->switch('switch', __('配置开关'))->default(1);
        $form->switch('notice_switch', __('通知开关'))->default(1);
        $form->switch('use_cloak', __('使用cloak'))->default(1);
        $form->text('location', __('排除地区'))->default('IN,RU,IE,TW,CN,HK');
        $form->text('allow_location', __('允许地区'))->default('US/CA');
        $form->text('notice', __('备注'))->default('无');

        return $form;
    }
}
